package com.geeker.module.sys.user.controller;

import com.geeker.common.annotation.Log;
import com.geeker.common.annotation.PreAuth;
import com.geeker.common.annotation.RepeatSubmit;
import com.geeker.common.api.CommonResult;
import com.geeker.common.enums.BusinessType;
import com.geeker.common.util.PageUtil;
import com.geeker.module.sys.user.domain.SysUser;
import com.geeker.module.sys.user.dto.SysUserDto;
import com.geeker.module.sys.user.sevice.SysUserService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name  = "用户管理")
@RequestMapping("/sys/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Operation(summary = "分页查询")
    @PostMapping("page")
    public CommonResult<Page<SysUser>> page(@RequestBody SysUserDto dto) {
        Page<SysUser> page = PageUtil.getPage(dto);
        Page<SysUser> iPage = sysUserService.page(page);
        return CommonResult.success(iPage);
    }

    @Operation(summary = "删除")
    @PostMapping("delete/{id}")
    @PreAuth(value = "sys:user:delete")
    public CommonResult<Boolean> delete(@PathVariable Integer id) {
        return CommonResult.success(sysUserService.removeById(id));
    }

    @Operation(summary = "详情")
    @PostMapping("detail/{id}")
    @PreAuth(value = "sys:user:detail")
    public CommonResult<SysUser> detail(@PathVariable Integer id) {
        return CommonResult.success(sysUserService.getById(id));
    }

    @Operation(summary = "新增")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("add")
    @PreAuth(value = "sys:user:add")
    @RepeatSubmit
    public CommonResult add(@RequestBody @Validated SysUser user) {
        sysUserService.add(user);
        return CommonResult.success();
    }
}
